<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class m_account extends CI_Model {

    var $iduser;
    var $username;
    var $password;
    var $fullname;
    var $lastlog;
    var $ava;

    public function __construct() {
        parent::__construct();
    }

    public function set_iduser($iduser) {
        $this->iduser = $iduser;
    }
    public function set_username($username) {
        $this->username = $username;
    }
    
    public function set_password($password) {
        $this->password = $password;
    }

    public function set_fullname($fullname) {
        $this->fullname = $fullname;
    }

    public function set_lastlog($lastlog) {
        $this->lastlog = $lastlog;
    }
    
    public function set_ava($ava) {
        $this->ava = $ava;
    }

    function select_a_record($param) {
        $sql = "SELECT * FROM user WHERE username_user = ? AND password_user = ?";
        $query = $this->db->query($sql, $param);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();

            $this->set_iduser($result['id_user']);
            $this->set_username($result['username_user']);
            $this->set_fullname($result['fullname_user']);
            $this->set_password($result['password_user']);
            $this->set_ava($result['user_ava']);
            $sql2 = "UPDATE user SET last_log = NOW() WHERE username_user = '$this->username'";
            $query2 = $this->db->query($sql2);
            $this->set_lastlog($result['last_log']);

            $this->make_cookie();
            return $result;
        } else {
          return false;
          
        }
    }
    

    protected function make_cookie() {
        $this->input->set_cookie('c_iduser',  $this->iduser,time()+3600);
        $this->input->set_cookie('c_username',  $this->username,time()+3600);
        $this->input->set_cookie('c_password',  $this->password,time()+3600);
        $this->input->set_cookie('c_fullname',  $this->fullname,time()+3600);
        $this->input->set_cookie('c_lastlog',  $this->lastlog,time()+3600);
        $this->input->set_cookie('c_ava',  $this->ava,time()+3600);
    }

    public function get_cookie() {
        return $_COOKIE;
    }
    public function delete_cookie() {
        $this->input->set_cookie('c_username','', time()-3600);
        $this->input->set_cookie('c_iduser','', time()-3600);
        $this->input->set_cookie('c_fullname','', time()-3600);
        $this->input->set_cookie('c_lastlog','', time()-3600);
        $this->input->set_cookie('c_ava','', time()-3600);        
    }
}
